package dk.iha;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.net.ParseException;

public class DateFormating {
	
	public static String HOURMINUTEFORMAT = "HH:mm";
	public static String DAYYEARTIMEFORMAT = "EEE MMM d yyyy HH:mm";
	public static String DAYYEARFORMAT = "EEE MMM d yyyy";
	public static String DEFAULT_FORMAT = "EEE MMM d HH:mm:ss Z yyyy";
	
	public static String getTimeInFormat(String dateString,String datePattern){
		//String dateString = "Sun Oct 07 23:37:47 CEST 2012";
		//EEE = day in week,MMM = month in year ,d = day in month,HH:mm:ss = clock, Z = timezone, yyyy = year 
		//SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
		//SharedData.getInstance().addDebugDataLog_t("dateString: "+ dateString + " datePattern: " + datePattern);
		//The original format - as saved in db
	    SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_FORMAT, Locale.US);
	    SimpleDateFormat newDateFormat = new SimpleDateFormat(datePattern, Locale.US);
	    Date convertedDate = null;
	    try {
	    	//SharedData.getInstance().addDebugDataLog_t("converteddate: " + convertedDate + "dateString: "+ dateString + " datePattern: " + datePattern);
	        convertedDate = dateFormat.parse(dateString);
	        //SharedData.getInstance().addDebugDataLog_t("converteddate: " + convertedDate + "dateString: "+ dateString + " datePattern: " + datePattern);
	        dateString = newDateFormat.format(convertedDate);
	        //SharedData.getInstance().addDebugDataLog_t("converteddate: " + convertedDate + "dateString: "+ dateString + " datePattern: " + datePattern);
	    } catch (ParseException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    } catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	   
		return dateString;  
	}
	
	public static Date dateFromString(String dateString){
		SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_FORMAT, Locale.US);
	    Date convertedDate = new Date();
	    try {
	        convertedDate = dateFormat.parse(dateString);
	    } catch (ParseException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    } catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return convertedDate;
	}

}
